class Bid < ActiveRecord::Base



  validates_presence_of :address, :business_unit, :prop_dept_contact, :project_type, :business_case, :on_target, :cashflow_qtr
  validates_numericality_of :year, :territory, :prop_ref, :cap_receipt, :cap_spend, :cap_retail_spend, :only_integer => true, :allow_nil => false, :message => " - Oh come on, whole numbers not decimals!"
  validates_numericality_of :area, :allow_nil => false
  validates_uniqueness_of :prop_ref
  validates_format_of :area, :with => /^[1-2]\.[1-5]$|^[3]\.[1-6]$|^[4]\.[1-5]$/, :message => " - What's wrong with you? Use normal Areas!"
  validates_format_of :territory, :with => /[1-4]/, :message => " - Wrong, there are four of them one to four"
  validates_format_of :year, :with => /2007|2008|2009/, :message => " - Wrong, You can only enter bids for years 2007, 2008 or 2009"
  validates_format_of :prop_ref, :with => /^[2-9][0-9]$|^[1-9][0-9][0-9]$|^[1-3][0-9][0-9][0-9]$/, :message => " - Wrong, use Propman Ref. not made-up number!"
  validates_inclusion_of :business_unit, :in => %w{ CES Ops Retail Ops/CT Ops/Retail Ops/CT/Terr CT Ops/CT/Options Ops/CT/Terr/UKO }, :message => " - Use normal Propman divisions"
  validates_inclusion_of :prop_dept_contact, :in => %w{ ALN MC GM AN MD SR/GP GP MJ GA AN/CH DE CH GP/AW GP/AN}, :message => " - Use only normal Top20 initials - no spaces, use / for AND"
  validates_inclusion_of :project_type, :in => %w{ A D C D/A A/C Sublet }, :message => " - Use only A, D, C, D/A or Sub"
  validates_inclusion_of :business_case, :in => %w{ Draft N/A Y N Awaited Approved }, :message => " - Use only Draft, N/A, Y, N or Awaited"
  validates_inclusion_of :on_target, :in => %w{ Draft Y N 2007 2008 2009 2010 Undecided }, :message => " - Use only Draft, Y, N, 2008, 2009, 2010 or Undecided"
  validates_inclusion_of :cashflow_qtr, :in => %w{ Q1 Q2 Q3 Q4 2008 2009 2010 }, :message => " - Use only Q1, Q2, Q3, Q4, 2008 2009 or 2010"
  validates_inclusion_of :completed, :in => %w{ Y N }, :message => " - Use only Y or N "
  
 
  
  protected
  def validate
    errors.add(:cap_receipt, " - Should be positive") if cap_receipt.nil? || cap_receipt < -0.01
    errors.add(:cap_spend, " - Should be positive") if cap_spend.nil? || cap_spend < -0.01
    errors.add(:cap_retail_spend, " - Should be positive") if cap_retail_spend.nil? || cap_retail_spend < -0.01
    
    if project_type == "A" && (cap_receipt != 0 || cap_retail_spend != 0)
      errors.add(:project_type, " - A only Capital Spend allowed")
    end
    
    if project_type == "D" && (cap_spend != 0 || cap_retail_spend != 0)
      errors.add(:project_type, " - D only Capital Receipt allowed")
    end
    
    if project_type == "C" && (cap_receipt != 0 )
      errors.add(:project_type, " - C only Capital Spend and Capital Retail Spend allowed")
    end
           
    if project_type == "Sub" && (cap_spend != 0 || cap_retail_spend != 0)
      errors.add(:project_type, " - Sub only Capital Receipt allowed")
    end
   
      
  end

end